package com.tidc.seaserverservice.service.impl;


import com.tidc.seaservermapper.dao.MenuDao;
import com.tidc.seaservermapper.dao.RoleDao;
import com.tidc.seaservermapper.dao.UserDao;
import com.tidc.seaservermodel.dto.UserDto;
import com.tidc.seaservermodel.entity.Menu;
import com.tidc.seaservermodel.entity.Role;
import com.tidc.seaserverservice.util.LogUtil;
import com.tidc.seaserverservice.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service
@Slf4j
public class MyUserAuthorizationServiceImpl implements UserDetailsService {
    @Resource
    private UserDao userDao;
    @Resource
    private UserUtil userUtil;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return userToUserDetail(username);
    }
    /**
     * 封装成UserDetail给security授权
     *
     * @return
     */
    private User userToUserDetail(String phone) {
        User res = null;
        com.tidc.seaservermodel.entity.User user = userDao.queryByPhone(phone);
//        if(user == null){
//            return new User("aaa","aaa",new ArrayList<>());
//        }
        UserDto userInfo = userUtil.getUserInfo(user);
        if(userInfo!=null){
            ArrayList<String> authorities = new ArrayList<>();
            List<Role> roles = userInfo.getRoles();
            // 为角色标识加上ROLE_前缀（Spring Security规范），并添加到权限集合
            roles.forEach(role -> authorities.add(role.getName()));
            //转成用逗号分隔的字符串，为用户设置权限标识。
            List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(
                    String.join(",", authorities)
            );
            boolean accountNonLocked = true;
            if (user.getIsDelete() == 1) {
                accountNonLocked = false;
            }
            res = new User(
                    // 用户名
                    userInfo.getUser().getPhone(),
                    // 密码
                    userInfo.getUser().getPassword(),
                    //  是否启用该账号,提示《用户已失效》
                    true,
                    // 指示用户的帐户是否已过期 提示《用户凭证已过期》
                    true,
                    // 指示用户的凭据(密码)是否已过期，提示《用户凭证已过期》
                    true,
                    // 非锁定用户，提示《该用户已被锁定》
                    accountNonLocked,
                    // 角色、url集合
                    grantedAuthorities
            );
        }
        return res;
    }
}
